package com.joesem.sale.client.base

import android.content.Context
import android.graphics.Color
import android.os.Build
import android.os.Bundle
import android.view.LayoutInflater
import android.view.WindowManager
import androidx.appcompat.app.AlertDialog
import androidx.databinding.DataBindingUtil.setContentView
import androidx.viewbinding.ViewBinding
import com.jiaxing.sale.client.R
import java.lang.reflect.ParameterizedType

abstract class BaseDialog<BD : ViewBinding>(
    context: Context,
    style: Int = R.style.Dialog
) : AlertDialog(context, style) {
    lateinit var bind: BD
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            window?.decorView?.systemUiVisibility =
                android.view.View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or android.view.View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
            window?.statusBarColor = Color.BLACK
        }
        window?.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
        window?.statusBarColor = 0x00000000
        window?.setLayout(
            WindowManager.LayoutParams.MATCH_PARENT,
            WindowManager.LayoutParams.WRAP_CONTENT
        )
        bindInit()

    }

    private fun bindInit() {
        try {
            this::class.java.genericSuperclass.let { it ->
                val type = ((it as ParameterizedType).actualTypeArguments[0]) as Class<*>
                type.getDeclaredMethod("inflate", LayoutInflater::class.java).let {
                    bind = it.invoke(null, layoutInflater) as BD
                    setContentView(bind.root)
                    bind.initView()
                }
            }
        } catch (e: Exception) {
        }
    }

    abstract fun BD.initView()

}